//
//  StatusCellRetweetedView.swift
//  Weobo
//
//  Created by 史亚洲 on 16/1/20.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit
import SnapKit

class StatusCellRetweetedView: UIView {
    //定义约束对象属性 来进行记录  Constraint 就是SnapKit中约束对象的类型
    var bottomCostant: Constraint?
    
    //MARK:定义外部变量
    var retweeted_status: Status? {
    
        didSet {
        //绑定数据
            retweetedLabel.text = retweeted_status?.text
            
            //1.通过这句代码,在没有配图的情况下,将配图视图的大小计算为CGSizeZero
            pictureView.imageURLs=retweeted_status?.imageURLs
            
            //2.卸载约束
            self.bottomCostant?.uninstall()
            
            //3.更新约束
            if let urls=retweeted_status?.imageURLs {
            
                print(urls.count)
                //3.1设置底部约束相对于配图视图的底部
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.bottomCostant=make.bottom.equalTo(pictureView.snp_bottom).offset(StatusCellMargin).constraint
                })
            
            
            } else {
                //3.2设置底部约束相对于转发微博正文
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.bottomCostant=make.bottom.equalTo(retweetedLabel.snp_bottom).offset(StatusCellMargin).constraint
                })
            
            
            
            }

        
        
        }
    
    
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //设置UI界面
    private func setupUI() {
        backgroundColor=UIColor(white: 0.9, alpha: 1.0)
        addSubview(retweetedLabel)
        addSubview(pictureView)
        
        //设置约束
        retweetedLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(self.snp_top).offset(StatusCellMargin)
            make.left.equalTo(self.snp_left).offset(StatusCellMargin)
        }
        pictureView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(retweetedLabel.snp_left)
            make.top.equalTo(retweetedLabel.snp_bottom).offset(StatusCellMargin)
            
        }
        
        //自动计算微博行高的一条约束
        self.snp_makeConstraints { (make) -> Void in
             self.bottomCostant=make.bottom.equalTo(pictureView.snp_bottom).offset(StatusCellMargin).constraint
        }
        
        

        
    }
    //MARK:懒加载视图
    private lazy var retweetedLabel: UILabel = UILabel(title: "转发微博:流水千年,你在为谁守候!", fontSize: 14, color: UIColor.darkGrayColor(),margin: StatusCellMargin)
    
    private lazy var pictureView: StatusCellPictureView = StatusCellPictureView()
    
}
